/**
 * @author zhouheng
 * @date 2022年03月19日 22:24
 */

package com.atguigu.gulimall.order.listener;

import com.atguigu.common.constant.RabbitInfo;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import java.io.IOException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author zhouheng
 * @Description 方法描述
 * 接收消息队列，是订单的队列，不是库存的队列
 * @Date 22:26 2022/3/19 0019
 * @Param
 * @return
 **/
@Service
@RabbitListener(queues = {RabbitInfo.Order.releaseQueue})
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    /*
     * @author zhouheng
     * 监听到延迟队列中消息超时，则进行关单操作
     * @date 2022/3/20 0020 16:51
     * @param null
     * @return null
     */
    @RabbitHandler
    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
        try {
            orderService.closeOrder(entity);
            // 手动调用支付宝收单
            // 手动确认消息消费，并告知队列将消息删除。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            // 关单失败，则将消息重新发往队列，让其他消费者去消费。
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }



}
